home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 4,401 to 4,500 / aol-file-protocol-4400-4401-to-4500.zip / AOLDLs / PDA-Newton Development / ND+ Programming Newton IR usi / howIR.txt next >
Text File  |  2014-12-08  |  6KB  |  101 lines

  1. NS BASIC Tech Note                                     Nov 30, 1994
  2.  
  3. How to control your TV, CD player, etc from NS BASIC on your Newton
  4. -------------------------------------------------------------------
  5.  
  6. 1.0 Introduction
  7. 2.0 Listing
  8. 3.0 Notes on Listing
  9. 4.0 IR Remote Control
  10.     4.1 Pioneer CD player codes
  11.     4.2 Finding codes for your own player
  12.  
  13.  
  14. 1.0 Introduction
  15. ----------------
  16.  
  17. It's pretty easy to write a program to control infrared remote controlled devices from NS BASIC. In this tech note, we'll show you how to do the basic commands to control the remote: it will be up to you to set up the proper codes for your own devices and make your own controlling program.
  18.  
  19. The following code sample shows how to issue a PLAY command to a Pioneer CD player. The first part of the program sets up the calling sequence; statements 1000 to 1060 actually transmit the command. You'll be changing lines 20 to 90 to suit your own device; feel free to copy lines 1000 to 1060 into your program.
  20.  
  21. In the next rev of NS BASIC, look for a built in function to take over most of  the work of the subroutine: this should work fine for now, however.
  22.  
  23.  
  24. 2.0 Listing
  25. -----------
  26.  
  27. 0010 rem test ir -- ir
  28. 0020 LET t="01000101101110101110100000010111"
  29. 0030 LET trans=[0,500,14,50,14,0,8]
  30. 0035 LET zero="0"[0] // char 0 (not string 0)
  31. 0040 for i=0 to strLen(t)-1
  32. 0050   LET x:=addArraySlot(trans,1)
  33. 0060   if t[i]=zero then x:=addArraySlot(trans,1) else x:=addArraySlot(trans,3)
  34. 0070 next i
  35. 0080 LET x:=addArraySlot(trans,1)
  36. 0090 LET x:=addArraySlot(trans,1)
  37. 0100 GOSUB 1000 //transmit to IR
  38. 0110 stop
  39. 1000 rem transmit to IR
  40. 1010 function makeCode(x) begin c:=setLength(clone(""),24+length(x)*4);for i:=0 to length(x)-1 do stuffLong(c,i*4,x[i]); c end
  41. 1020 LET trans[5]=length(trans)-6
  42. 1030 LET ir=openRemoteControl()
  43. 1040 LET x:=sendRemoteControlCode(ir,u:makecode(trans),1)
  44. 1050 LET x:=closeRemoteControl(ir)
  45. 1060 return
  46.  
  47.  
  48. 3.0 Notes on Listing
  49. --------------------
  50.  
  51. In the above example, trans is an array which contains the command string to be sent out the IR port. The following elements make up the array:
  52.  
  53. trans[0]    Command name code; put anything you like in here.
  54. trans[1]    TimeBase: number of milliseconds in each time unit
  55. trans[2]    Lead In: number of time units to transmit before starting command
  56. trans[3]    Repeat: number of time units to wait before repeating command
  57. trans[4]    Lead Out: number of time units after the command
  58. trans[5]    always zero.
  59. trans[6...] A sequence of numbers, showing the number of time units
  60.             to spend in each state, starting with off.
  61.  
  62.  
  63. Some notes about the subroutine:
  64.  
  65. The third argument in line 1040 is the number of times to repeat the command. This is usually just one, but for certain commands (and certain manufacturer's remote units) you may want to make this a parameter.
  66.  
  67. If you open the IR (as in line 1030), make sure you do the close (as in 1050). The variable IR returned by the openRemoteControl() command is a magic number: if you lose it, the only way to close the IR remote control will be to reset your Newton.
  68.  
  69.  
  70. 4.0 How IR Remote Controls work
  71. -------------------------------
  72. IR remote controls are really quite simple: A sequence of flashes, much like signalling with a flashlight, is transmitted. Each command has a different pattern of lengths of time the signal is on or off. Each manufacturer has his own pattern; sometimes different devices from the same manufacturer will even have different patterns.
  73.  
  74. Commands are usually expressed as hex numbers. By converting these to binary, you get the sequence of ONs and OFFs to be sent. Along with the codes, the manufacturer will specify how long the ON and OFF periods have to be to define each of the characters.
  75.  
  76. These periods are usually expressed as a number of time units. Each time unit is a number of milliseconds. There may also be a period of time that the signal is sent as a lead in or lead out to the command.
  77.  
  78. 4.1 How the Pioneer CD control works
  79. ------------------------------------
  80. The Pioneer does everything in multiples of 500 milliseconds, so trans[1] is set to 500. For a command, the first thing that is expected is a lead in of 14 time units, set in trans[2], followed by no signal for 8 time units, which is in trans[6].
  81.  
  82. The command itself is made up of two parts: a lead in code, which is fixed, and the command code itself. For the the Pioneer CD player, the lead in code is 45BA (base 16) and the command for PLAY is E817. Converting these to binary gives the string "01000101101110101110100000010111" used in the above example.
  83.  
  84. Each binary digit in the Pioneer is transmitted as single time unit ON, followed by a single time unit OFF for 0 and three time units for 1. The example program above is a quick and dirty method for building up the array of ones and threes that is needed: I'm sure more elegant code can be written. In particular, building up the trans array takes too long to be done each time. A nice application could store the completed trans arrays in a file.
  85.  
  86. The end result is the trans[7] and on are a sequence of time units that the signal is to be alternately ON and OFF, starting with ON.
  87.  
  88. For the record (or CD that is), the other codes used on Pioneer CD players are STOP (6897), REW (8877), FF (08F7), PAUSE (18E7), PGM (B04F), TRACK (02FD) and DISC (B847, also 827D)
  89.  
  90. 4.2 How do I find the codes for my own player?
  91. ----------------------------------------------
  92. The easiest way is through the internet: ftp to 
  93. nada.kth.se:home/d89-bga/hp/remote/remotes
  94.  
  95. Many of the common units are documented at this site. If you make a sample bit of code for a new type of remote, email it to us at gh@hookup.net, and we'll put it in our code samples library for all to use.
  96.  
  97. If all else fails, you can always hook up a diode to an oscilliscope and capture the codes from another remote. It doesn't seem to be possible to collect the code directly from another remote to the Newton: the incoming IR filters out frequencies other than those needed for Sharp-type data transmission. This rules out the frequency used by IR remote devices.
  98.  
  99. Now if only the Newton was backlit so we could see our remotes by the light of the TV!
  100.  
  101. NS BASIC is BASIC for the Newton. For more information, please contact NS BASIC Corp. at 416 264-5999 or at gh@hookup.net.